<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="迎着朝阳的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    RestTemplate使用总结 |  迎着朝阳
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="https://dxysun.com/static/yan.png" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?aa994a8d65700b8835787dd39d079d7e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-springBootForRestTemplate"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  RestTemplate使用总结
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2018/05/22/springBootForRestTemplate/" class="article-date">
  <time datetime="2018-05-22T11:06:27.000Z" itemprop="datePublished">2018-05-22</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">8 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="RestTemplate简介"><a href="#RestTemplate简介" class="headerlink" title="RestTemplate简介"></a>RestTemplate简介</h1><p>近几年来,以信息为中心的表述性状态转移(Representational State Transfer，REST)已经称为替代传统SOAP Web 服务的流行方案.<br>REST是将资源的状态以最适合客户端或服务端的形式从服务器端转移到客户端（或者反过来）</p>
<a id="more"></a>
<p>在REST中，资源通过URL进行识别和定位。至于RESTful URL的结构并没有严格的规则，但是 URL应该能够识别资源，而不是简单的发一条命令到服务器上。<br>借助 RestTemplate，Spring应用能够方便地使用REST资源<br>Spring的 RestTemplate访问使用了模版方法的设计模式.<br>模版方法将过程中与特定实现相关的部分委托给接口,而这个接口的不同实现定义了接口的不同行为.<br>RestTemplate定义了36个与REST资源交互的方法，其中的大多数都对应于HTTP的方法。<br>其实，这里面只有11个独立的方法，其中有十个有三种重载形式，而第十一个则重载了六次，这样一共形成了36个方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">delete()   &#x2F;&#x2F;在特定的URL上对资源执行HTTP DELETE操作</span><br><span class="line">exchange() &#x2F;&#x2F;在URL上执行特定的HTTP方法，返回包含对象的ResponseEntity，这个对象是从响应体中映射得到的</span><br><span class="line">execute() &#x2F;&#x2F;在URL上执行特定的HTTP方法，返回一个从响应体映射得到的对象</span><br><span class="line">getForEntity() &#x2F;&#x2F;发送一个HTTP GET请求，返回的ResponseEntity包含了响应体所映射成的对象</span><br><span class="line">getForObject() &#x2F;&#x2F;发送一个HTTP GET请求，返回的请求体将映射为一个对象</span><br><span class="line">postForEntity()  &#x2F;&#x2F;POST 数据到一个URL，返回包含一个对象的ResponseEntity，这个对象是从响应体中映射得到的</span><br><span class="line">postForObject()  &#x2F;&#x2F; POST 数据到一个URL，返回根据响应体匹配形成的对象</span><br><span class="line">headForHeaders()  &#x2F;&#x2F;发送HTTP HEAD请求，返回包含特定资源URL的HTTP头</span><br><span class="line">optionsForAllow()  &#x2F;&#x2F;发送HTTP OPTIONS请求，返回对特定URL的Allow头信息</span><br><span class="line">postForLocation()  &#x2F;&#x2F; POST 数据到一个URL，返回新创建资源的URL</span><br><span class="line">put()  &#x2F;&#x2F; PUT 资源到特定的URL</span><br></pre></td></tr></table></figure>
<blockquote>
<p>实际上,由于Post 操作的非幂等性,它几乎可以代替其他的CRUD操作.</p>
</blockquote>
<h1 id="如何使用"><a href="#如何使用" class="headerlink" title="如何使用"></a>如何使用</h1><p> SpringBoot 项目，创建RestTemplate的配置类RestTemplateConfig</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">@Configuration</span><br><span class="line">public class RestTemplateConfig &#123;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public RestTemplate restTemplate(ClientHttpRequestFactory factory)&#123;</span><br><span class="line">        return new RestTemplate(factory);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public ClientHttpRequestFactory simpleClientHttpRequestFactory()&#123;</span><br><span class="line">        SimpleClientHttpRequestFactory factory &#x3D; new SimpleClientHttpRequestFactory();</span><br><span class="line">        factory.setReadTimeout(5000);&#x2F;&#x2F;单位为ms</span><br><span class="line">        factory.setConnectTimeout(5000);&#x2F;&#x2F;单位为ms</span><br><span class="line">        return factory;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在Controller里注入RestTemplate</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">@Autowired</span><br><span class="line">private RestTemplate restTemplate;</span><br></pre></td></tr></table></figure>

<h1 id="Get请求"><a href="#Get请求" class="headerlink" title="Get请求"></a>Get请求</h1><p>在 RestTemplate中，发送一个GET请求，我们可以通过如下两种方式</p>
<h2 id="getForEntity"><a href="#getForEntity" class="headerlink" title="getForEntity"></a>getForEntity</h2><p><img src="http://qiniu.dxysun.com/18-5-22/93272646.jpg" alt=""><br>getForEntity 方法的返回值是一个ResponseEntity<T>，ResponseEntity<T>是 Spring 对 HTTP 请求响应的封装，包括了几个重要的元素，如响应码、contentType、contentLength、响应消息体等。比如下面一个例子</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;hello&quot;)</span><br><span class="line">public String getHello() &#123;</span><br><span class="line">    ResponseEntity&lt;String&gt; responseEntity &#x3D; restTemplate.getForEntity(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&quot;, String.class);</span><br><span class="line">    String body &#x3D; responseEntity.getBody();</span><br><span class="line">    HttpStatus statusCode &#x3D; responseEntity.getStatusCode();</span><br><span class="line">    int statusCodeValue &#x3D; responseEntity.getStatusCodeValue();</span><br><span class="line">    HttpHeaders headers &#x3D; responseEntity.getHeaders();</span><br><span class="line">    StringBuffer result &#x3D; new StringBuffer();</span><br><span class="line">    result.append(&quot;responseEntity.getBody()：&quot;).append(body).append(&quot;&lt;hr&gt;&quot;)</span><br><span class="line">            .append(&quot;responseEntity.getStatusCode()：&quot;).append(statusCode).append(&quot;&lt;hr&gt;&quot;)</span><br><span class="line">            .append(&quot;responseEntity.getStatusCodeValue()：&quot;).append(statusCodeValue).append(&quot;&lt;hr&gt;&quot;)</span><br><span class="line">            .append(&quot;responseEntity.getHeaders()：&quot;).append(headers).append(&quot;&lt;hr&gt;&quot;);</span><br><span class="line">    return result.toString();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>getForEntity 的第一个参数为要调用的服务的地址，getForEntity第二个参数 String.class 表示希望返回的 body 类型是 String</p>
</blockquote>
<p>输出结果<br><img src="http://qiniu.dxysun.com/18-5-22/95398174.jpg" alt=""></p>
<p>在调用服务提供者提供的接口时，可能需要传递参数，有两种不同的方式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;hello1&quot;)</span><br><span class="line">public String Hello() &#123;</span><br><span class="line">    &#x2F;&#x2F; String name &#x3D; &quot;张三&quot;;</span><br><span class="line">    &#x2F;&#x2F; ResponseEntity&lt;String&gt; responseEntity &#x3D; restTemplate.getForEntity(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello?name&#x3D;&#123;name&#125;&quot;, String.class, name);</span><br><span class="line">    ResponseEntity&lt;String&gt; responseEntity &#x3D; restTemplate.getForEntity(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello?name&#x3D;&#123;1&#125;&quot;, String.class, &quot;张三&quot;);</span><br><span class="line">    return responseEntity.getBody();</span><br><span class="line">&#125;</span><br><span class="line">@RequestMapping(&quot;&#x2F;hello2&quot;)</span><br><span class="line">public String Hello2() &#123;</span><br><span class="line">    Map&lt;String, String&gt; map &#x3D; new HashMap&lt;&gt;();</span><br><span class="line">    map.put(&quot;name&quot;, &quot;李四&quot;);</span><br><span class="line">    ResponseEntity&lt;String&gt; responseEntity &#x3D; restTemplate.getForEntity(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello?name&#x3D;&#123;name&#125;&quot;, String.class, map);</span><br><span class="line">    return responseEntity.getBody();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以用一个数字做占位符，最后是一个可变长度的参数，来{}替换前面的占位符,也可以前面使用 name={name} 这种形式，最后一个参数是一个 map，map 的 key 即为前边占位符的名字，map的 value 为参数值</p>
</blockquote>
<p>服务提供者不仅可以返回String，也可以返回一个自定义类型的对象，比如服务提供者中有如下方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(value &#x3D; &quot;&#x2F;getbook&quot;, method &#x3D; RequestMethod.GET)</span><br><span class="line">public Book book1() &#123;</span><br><span class="line">  return new Book(&quot;三国演义&quot;, 90, &quot;罗贯中&quot;, &quot;花城出版社&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在服务消费者中通过如下方式来调用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;book&quot;)</span><br><span class="line">public Book book1() &#123;</span><br><span class="line">  ResponseEntity&lt;Book&gt; responseEntity &#x3D; restTemplate.getForEntity(&quot;http:&#x2F;&#x2F;HELLO-SERVICE&#x2F;getbook1&quot;, Book.class);</span><br><span class="line">  return responseEntity.getBody();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="getForObject"><a href="#getForObject" class="headerlink" title="getForObject"></a>getForObject</h2><p><img src="http://qiniu.dxysun.com/18-5-22/63316485.jpg" alt=""></p>
<blockquote>
<p>getForObject 函数实际上是对 getForEntity 函数的进一步封装，如果你只关注返回的消息体的内容，对其他信息都不关注，此时可以使用 getForObject，例子如下</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;hello2&quot;)</span><br><span class="line">public String Hello2() &#123;</span><br><span class="line">    Map&lt;String, String&gt; map &#x3D; new HashMap&lt;&gt;();</span><br><span class="line">    map.put(&quot;name&quot;, &quot;李四&quot;);</span><br><span class="line">    String responseEntity &#x3D; restTemplate.getForObject(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello?name&#x3D;&#123;name&#125;&quot;, String.class, map);</span><br><span class="line">    return responseEntity;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>其他用法与getForEntity类似</p>
</blockquote>
<h1 id="Post请求"><a href="#Post请求" class="headerlink" title="Post请求"></a>Post请求</h1><p>在 RestTemplate 中，POST 请求可以通过如下三个方法来发起</p>
<h2 id="postForEntity"><a href="#postForEntity" class="headerlink" title="postForEntity"></a>postForEntity</h2><p><img src="http://qiniu.dxysun.com/18-5-22/43930072.jpg" alt=""><br>该方法和get请求中的getForEntity方法类似，如下例子</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;hello3&quot;)</span><br><span class="line">public String Hello3() &#123;</span><br><span class="line">    String[] args &#x3D; &#123;&quot;arg1&quot;,&quot;arg2&quot;,&quot;arg3&quot;&#125;;</span><br><span class="line"></span><br><span class="line">    HashMap&lt;String,Object&gt; postData &#x3D; new LinkedHashMap&lt;&gt;();</span><br><span class="line">    postData.put(&quot;name&quot;, &quot;dxy&quot;);</span><br><span class="line">    postData.put(&quot;args&quot;, args);</span><br><span class="line">    ResponseEntity&lt;String&gt; responseEntity &#x3D; restTemplate.postForEntity(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello&quot;, postData, String.class);</span><br><span class="line">    return responseEntity.getBody();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>方法的第一参数表示要调用的服务的地址,方法的第二个参数表示上传的参数,方法的第三个参数表示返回的消息体的数据类型,特别注意上传的参数里如果有列表参数，则可以用String[]来存放列表</p>
</blockquote>
<h2 id="postForObject"><a href="#postForObject" class="headerlink" title="postForObject"></a>postForObject</h2><p><img src="http://qiniu.dxysun.com/18-5-22/46128215.jpg" alt=""><br>如果你只关注，返回的消息体，可以直接使用postForObject。用法和getForObject一致。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;&#x2F;hello3&quot;)</span><br><span class="line">public String Hello3() &#123;</span><br><span class="line">    String[] args &#x3D; &#123;&quot;arg1&quot;,&quot;arg2&quot;,&quot;arg3&quot;&#125;;</span><br><span class="line"></span><br><span class="line">    HashMap&lt;String,Object&gt; postData &#x3D; new LinkedHashMap&lt;&gt;();</span><br><span class="line">    postData.put(&quot;name&quot;, &quot;dxy&quot;);</span><br><span class="line">    postData.put(&quot;args&quot;, args);</span><br><span class="line">    String responseEntity &#x3D; restTemplate.postForObject(&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;spark&#x2F;hello&quot;, postData, String.class);</span><br><span class="line">    return responseEntity;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="postForLocation"><a href="#postForLocation" class="headerlink" title="postForLocation"></a>postForLocation</h2><p><img src="http://qiniu.dxysun.com/18-5-22/1231568.jpg" alt=""></p>
<blockquote>
<p>postForLocation 也是提交新资源，提交成功之后，返回新资源的 URI，postForLocation 的参数和前面两种的参数基本一致，只不过该方法的返回值为 URI ，这个只需要服务提供者返回一个 URI 即可，该 URI 表示新资源的位置。</p>
</blockquote>
<h1 id="PUT-请求"><a href="#PUT-请求" class="headerlink" title="PUT 请求"></a>PUT 请求</h1><p><img src="http://qiniu.dxysun.com/18-5-22/50924451.jpg" alt=""></p>
<blockquote>
<p>在 RestTemplate 中，PUT 请求可以通过 put 方法调用，put 方法的参数和前面介绍的 postForEntity 方法的参数基本一致，只是 put 方法没有返回值而已</p>
</blockquote>
<h1 id="DELETE-请求"><a href="#DELETE-请求" class="headerlink" title="DELETE 请求"></a>DELETE 请求</h1><p><img src="http://qiniu.dxysun.com/18-5-22/63725121.jpg" alt=""></p>
<blockquote>
<p>与put请求类似</p>
</blockquote>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>基本上RestTemplate常用的方法都总结了，说实话，RestTemplate用起来真方便</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://dxysun.com/2018/05/22/springBootForRestTemplate/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/springboot/" rel="tag">springboot</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2018/05/28/hadoopForWebHdfs/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Hadoop WebHDFS设置和使用
          
        </div>
      </a>
    
    
      <a href="/2018/05/04/sparkAndHadoopForError/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">spark-shell在yarn模式下启动遇到的问题解决</div>
      </a>
    
  </nav>

  
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2024
        <i class="ri-heart-fill heart_icon"></i> dxysun
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
        <li>
          <a href="https://beian.miit.gov.cn" target="_black" rel="nofollow">豫ICP备17012675号-1</a>
        </li>
        
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="https://dxysun.com/static/logo.png" alt="迎着朝阳"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/alipay-20201219151322.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/weixin-20201219151346.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>